/**
 * <p>Copyright:Copyright(c) 2019</p>
 * <p>Company:Professional</p>
 * <p>Package:org.cesgroup.course.web</p>
 * <p>File:Ch003Controller.java</p>
 * <p>类更新历史信息</p>
 *
 * @todo chanlong(陈龙) 创建于 Sep 10, 2019 4:36:09 PM
 */
package org.cesgroup.course.controller;

import cn.hutool.core.io.FileUtil;
import com.google.common.collect.Lists;
import org.cesgroup.course.converter.Converter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>描述: </p>
 * <p>Company: Professional</p>
 *
 * @author chanlong(陈龙)
 * @version 1.0.2019
 * @date Sep 10, 2019 4:36:09 PM
 */
@RestController
public class Ch003Controller {

    @PostMapping("/word/2/html/2pdf")
    public Object word2Html2Pdf(final MultipartFile file) {
        try {
            // 启动责任链         //  明确目的， 先word->html->pdf
            String filepath = (String) converter.process(file.getBytes(), Lists.newArrayList(FileUtil.extName(file.getOriginalFilename()), "pdf"));

            byte[] data = FileUtil.readBytes(filepath);

            HttpHeaders headers = new HttpHeaders();
            headers.setContentDispositionFormData("attachment", "demo.pdf");
            headers.setContentType(MediaType.APPLICATION_PDF);
            return new ResponseEntity<byte[]>(data, headers, HttpStatus.OK);
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }

    @PostMapping("/word/2/html")
    public Object word2Html(final MultipartFile file) {
        try {
            // 启动责任链         //  明确目的， 先word->html->pdf
            String filepath = (String) converter.process(file.getBytes(), Lists.newArrayList(FileUtil.extName(file.getOriginalFilename())));

            byte[] data = FileUtil.readBytes(filepath);

            HttpHeaders headers = new HttpHeaders();
            headers.setContentDispositionFormData("attachment", "demo.html");
            headers.setContentType(MediaType.TEXT_HTML);
            return new ResponseEntity<byte[]>(data, headers, HttpStatus.OK);
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }

    // 注入“Converter”bean
    @Autowired
    private Converter converter;
}
